package html;

import common.OutputProvider;
import java.io.Writer;
import templates.PropertyTemplate;

public class XHtmlFormatter extends OutputProvider {

    public XHtmlFormatter(Writer out) {
        super(out);
    }

    public void startHtml() {
        echo("<html>");
    }

    public void endHtml() {
        echo("</html>");
    }

    public void startHead() {
        echo("<head>");
    }

    public void endHead() {
        echo("</head>");
    }

    public void startTitle() {
        echo("<title>");
    }

    public void endTitle() {
        echo("</title>");
    }

    public void writeTitle(String content) {
        startTitle();
        echo(content);
        endTitle();
    }

    public void writeMeta(String httpEquiv, String content) {
        echo("<meta http-equiv=\"" + httpEquiv + "\" content=\"" + content + "\" />");
    }

    public void writeLink(String rel, String type, String href) {
        echo("<link rel=\"" + rel + "\" type=\"" + type + "\" href=\"" + href + "\" />");
    }

    public void startBody() {
        echo("<body>");
    }

    public void endBody() {
        echo("</body>");
    }

    public void startH1() {
        echo("<h1>");
    }

    public void endH1() {
        echo("</h1>");
    }

    public void startH2() {
        echo("<h2>");
    }

    public void endH2() {
        echo("</h2>");
    }

    public void writeH1(String content) {
        startH1();
        echo(content);
        endH1();
    }

    public void writeH2(String content) {
        startH2();
        echo(content);
        endH2();
    }

    public void startTable() {
        echo("<table>");
    }

    public void endTable() {
        echo("</table>");
    }

    public void startTr(String styleClass) {
        StringBuilder builder = new StringBuilder("<tr");
        if (!styleClass.isEmpty()) {
            builder.append(" class=\"");
            builder.append(styleClass);
            builder.append("\"");
        }        
        builder.append(">");
        echo(builder.toString());
    }

    public void startTr() {
        startTr("");
    }
    
    public void endTr() {
        echo("</tr>");
    }

    public void startTd() {
        echo("<td>");
    }

    public void endTd() {
        echo("</td>");
    }

    public void startTh() {
        echo("<th>");
    }
    
    public void endTh() {
        echo("</th>");
    }
    
    public void startForm(String name, String action, String method) {
        StringBuilder builder = new StringBuilder("<form");
        if (!name.isEmpty()) {
            builder.append(" name=\"");
            builder.append(name);
            builder.append("\"");
        }
        if (!action.isEmpty()) {
            builder.append(" action=\"");
            builder.append(action);
            builder.append("\"");
        }
        if (!method.isEmpty()) {
            builder.append(" method=\"");
            builder.append(method);
            builder.append("\"");
        }
        builder.append(">");
        echo(builder.toString());
    }

    public void startForm(String action, String method) {
        startForm("", action, method);
    }

    public void endForm() {
        echo("</form>");
    }

    public void writeInput(InputType type, String name, String id, String styleClass, String value) {
        StringBuilder builder = new StringBuilder("<input");
        builder.append(" type=\"");
        builder.append(type.name());
        builder.append("\"");

        if (!name.isEmpty()) {
            builder.append(" name=\"");
            builder.append(name);
            builder.append("\"");
        }        
        
        if (!id.isEmpty()) {
            builder.append(" id=\"");
            builder.append(id);
            builder.append("\"");
        }        
        
        if (!styleClass.isEmpty()) {
            builder.append(" class=\"");
            builder.append(styleClass);
            builder.append("\"");
        }        
        
        if (!value.isEmpty()) {
            builder.append(" value=\"");
            builder.append(value);
            builder.append("\"");
        }
        
        builder.append(">");
        echo(builder.toString());
    }

    public void writeCheckbox(String name, String value, String styleClass, String onChecked, String onDblClick, boolean isChecked) {
        StringBuilder builder = new StringBuilder("<input type=\"checkbox\"");

        if (!name.isEmpty()) {
            builder.append(" name=\"");
            builder.append(name);
            builder.append("\"");
        }        
        
        if (!value.isEmpty()) {
            builder.append(" value=\"");
            builder.append(value);
            builder.append("\"");
        }

        if (!styleClass.isEmpty()) {
            builder.append(" class=\"");
            builder.append(styleClass);
            builder.append("\"");
        }

        if (!onChecked.isEmpty()) {
            builder.append(" onchange=\"");
            builder.append(onChecked);
            builder.append("\"");
        }

        if (!onDblClick.isEmpty()) {
            builder.append(" ondblclick=\"");
            builder.append(onDblClick);
            builder.append("\"");
        }

        if (isChecked) {
            builder.append(" checked=\"checked\"");
        }               
        
        builder.append(">");
        echo(builder.toString());
    }
    
    public void startDiv(String id, String styleClass) {
        StringBuilder builder = new StringBuilder("<div");
        if (!id.isEmpty()) {
            builder.append(" id=\"");
            builder.append(id);
            builder.append("\"");
        }
        if (!styleClass.isEmpty()) {
            builder.append(" class=\"");
            builder.append(styleClass);
            builder.append("\"");
        }
        builder.append(">");
        echo(builder.toString());
    }

    public void startDiv(String id) {
        startDiv(id, "");
    }

    public void startDiv() {
        startDiv("", "");
    }

    public void endDiv() {
        echo("</div>");
    }

    public void startP(String id, String styleClass) {
        StringBuilder builder = new StringBuilder("<p");
        if (!id.isEmpty()) {
            builder.append(" id=\"");
            builder.append(id);
            builder.append("\"");
        }
        if (!styleClass.isEmpty()) {
            builder.append(" class=\"");
            builder.append(styleClass);
            builder.append("\"");
        }
        builder.append(">");
        echo(builder.toString());
    }

    public void endP() {
        echo("</p>");
    }

    public void writeP(String id, String styleClass, String content) {
        startP(id, styleClass);
        echo(content);
        endP();
    }

    public void writeP(String styleClass, String content) {
        writeP("", styleClass, content);
    }

    public void getSelectPropertyList(Iterable<PropertyTemplate> list) {
        echo("<select class=\"inline\" id=\"nameFindingFild\" name=\"nameFindingFild\" size=\"1\">");
        for (PropertyTemplate o : list) {
            PropertyTemplate obj = (PropertyTemplate) o;
            putSelectOption(obj.getName(), obj.getName(), false);

        }
    }

    public void startSelect(String name, String styleClass) {
        echo("<select name=\"" + name + "\" class=\"" + styleClass + "\" size=\"1\">");
    }

    public void endSelect() {
        echo("</select>");
    }

    public void putSelectOption(String value, String caption, boolean isSelected) {
        if (isSelected) {
            echo("<option selected=\"selected\" value=\"" + value + "\">" + caption + "</option>");
        } else {
            echo("<option value=\"" + value + "\">" + caption + "</option>");
        }
    }

    public void putImage(String src, String styleClass) {
        echo("<img src=\"" + src + "\" class=\"" + styleClass + "\" />");
    }

    public void putWarning(String content) {
        startDiv("", "warning");
        echo("<img src=\"alert.png\" class=\"popupImage\" />");
        echo(content);
        endDiv();
    }

    public void putError(String content) {
        startDiv("", "error");
        echo("<img src=\"error.png\" class=\"popupImage\" />");
        echo(content);
        endDiv();
    }

    public void putSuccess(String content) {
        startDiv("", "success");
        echo("<img src=\"select.png\" class=\"popupImage\" />");
        echo(content);
        endDiv();
    }

    public void startScript(String type) {
        echo("<script type=\"" + type + "\">");
    }

    public void endScript() {
        echo("</script>");
    }

    public void writeScript(String type, String src) {
        StringBuilder builder = new StringBuilder("<script");
        if (!type.isEmpty()) {
            builder.append(" type=\"").append(type).append("\"");
        }
        if (!src.isEmpty()) {
            builder.append(" src=\"").append(src).append("\"");
        }
        builder.append("></script>");
        echo(builder.toString());
    }

    public void startA(String href, String target) {
        echo("<a href=\"" + href + "\" target=\"" + target + "\">");
    }

    public void endA() {
        echo("</a>");
    }

}
